package com.example.weather.interfaceimpl;

import android.content.Context;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.weather.R;
import com.example.weather.utils.GridViewInterface;

import java.util.ArrayList;
import java.util.Map;

/**
 * Created by hanjun on 2016/7/31.
 */
public class CalendarOperator implements GridViewInterface {

    private Map<String,Object> map;
    private String mDateString;
    private String mLunarDateString;
    private boolean isExist;
    private boolean isWeekend;
    private boolean isHavePlan;

    public View handle(View convertView, ViewGroup parent, int position,
                       Context context, ArrayList<Map<String, Object>> list, Object object){
        ViewHolder holder;
        map = list.get(position);
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(R.layout.calendar_item,parent, false);
            holder = new ViewHolder();
            holder.mShowDateTextView = (TextView)convertView.findViewById(R.id.tv_date_show);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        isExist = (boolean)map.get("isExist");
        if(isExist) {
            mDateString = (String)map.get("Date");
            mLunarDateString = (String)map.get("LunarDate");
            isWeekend = (boolean)map.get("isWeekend");
            isHavePlan = (boolean)map.get("isHavePlan");
            SpannableString sp = new SpannableString(mDateString + "\n" + mLunarDateString);
            sp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, mDateString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            sp.setSpan(new RelativeSizeSpan(1.2f), 0, mDateString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            if (mLunarDateString != null || mLunarDateString != "") {
                sp.setSpan(new RelativeSizeSpan(0.75f), mDateString.length() + 1, mLunarDateString.length() + mDateString.length()+1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            holder.mShowDateTextView.setText(sp);
            holder.mShowDateTextView.setTextColor(Color.BLACK);
            if(isHavePlan){
                holder.mShowDateTextView.setBackgroundColor(context.getResources().getColor(R.color.red));
            }else{
                if(isWeekend){
                    holder.mShowDateTextView.setBackgroundColor(context.getResources().getColor(R.color.grey));
                }else{
                    holder.mShowDateTextView.setBackgroundColor(context.getResources().getColor(R.color.white));
                }
            }


        }else{
            holder.mShowDateTextView.setText("");
            holder.mShowDateTextView.setBackgroundColor(context.getResources().getColor(R.color.white));

        }
        return convertView;
    }

    public class ViewHolder{
        public TextView mShowDateTextView;
    }

}
